<?php
namespace app\stores\model\promotion;

use app\common\model\CommonModel;

use app\common\model\MemberMoneyDetailModel;
use app\stores\model\order\GoodsOrderModel;
use app\stores\model\withdraw\MemberWithdrawModel;
use think\facade\Db;

class PromotionmoneydetailModel extends CommonModel{
    protected  $name = 'promotion_money_detail';
    protected  $pk   = 'id';
    public static function onBeforeInsert($model)
    {
        $model->set("add_time", date('Y-m-d H:i:s'));
        $model->set("add_ip", request()->ip());
    }
    public static function list($where,$order,$page=1,$limit=10,$field='*'){
        $query = Db::name('promotion_money_detail')
            ->where('money','<>',0)
            ->when(isset($where['team_id']),function ($query) use ($where){
                $query->where(['team_id'=>$where['team_id']]);
            })
            ->when(isset($where['member_id']),function ($query) use ($where){
                $query->where(['member_id'=>$where['member_id']]);
            })
            ->when(isset($where['type']),function ($query) use ($where){
                $query->where(['type'=>$where['type']]);
            })
            ->when(isset($where['status']),function ($query) use ($where){
                $query->where(['status'=>$where['status']]);
            })
            ->when(isset($where['is_ing']),function ($query) use ($where){
                $query->where(['withdraw_id'=>0]);
            })
            ->when(isset($where['add_start_time']),function ($query) use ($where){
                $query->where('add_time','>=',$where['add_start_time']);
            })
            ->when(isset($where['add_end_time']),function ($query) use ($where){
                $query->where('add_time','<=',$where['add_end_time']);
            })
            ->when(!empty($where['is_effect']),function ($query) use ($where){
                $query->where(['is_effect'=>$where['is_effect']]);
            })
            ->when(isset($where['name']),function ($query) use ($where){
                $query->where('name','like','%'.$where['name'].'%');
            })
            ->when(isset($where['withdraw_ids']),function ($query) use ($where){
                $query->whereNotIn('withdraw_id',$where['withdraw_ids']);
            })
            ->field($field);
        $count = $query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        if(!empty($list)){
            foreach ($list as &$v){
                $v['explain'] = $v['withdraw_status'] = null;
                if($v['type']==1){
                    if($v['order_id']>0){
                        $order = GoodsOrderModel::user_order(['order_id'=>$v['order_id']]);
                        $v['explain'] = $order?'推广收益-'.$order['nick_name']:null;
                    }
                    if($v['withdraw_id']>0){
                        $m_w = MemberWithdrawModel::info(['withdraw_id'=>$v['withdraw_id'],'is_delete'=>0]);
                        $v['explain'] = $m_w?'提现失败退回':null;
                    }

                }
                if($v['type']<1){
                    if($v['withdraw_id']>0){
                        $m_w = MemberWithdrawModel::info(['withdraw_id'=>$v['withdraw_id'],'is_delete'=>0]);
                        if($m_w['status']==0){
                            $v['withdraw_status'] = '审核中';
                        }
                        if($m_w['status']<0){
                            $v['withdraw_status'] = '失败';
                        }
                        if($m_w['status']==1){
                            $v['withdraw_status'] = '成功';
                        }
                        if($m_w['status']==2){
                            $v['withdraw_status'] = '待打款';
                        }
                        $v['explain'] = $m_w?'提现':null;
                    }
                }
            }
        }
        return compact('count','list');
    }
    public static function up($data,$where){
        $data['edit_time']=date('Y-m-d H:i:s');
        $res=self::update($data,$where);
        return $res;
    }
    public static function data_statis($where,$tj_where=[],$team_id=0){
        //除开提现失败退回
        if($team_id>0){
            $withdraw_ids = MemberWithdrawModel::where(['team_id'=>$team_id,'is_delete'=>0,'status'=>-1])->column('withdraw_id');
            $where[]=['withdraw_id','not in',$withdraw_ids];
        }
        $where[]=['type','=',1];
        $all_num=self::where($where)->count();
        $all_money=sprintf("%.2f",self::where($where)->sum('money'));
        $where[]=['add_time','>=',date('Y-m-d 00:00:00')];
        $where[]=['add_time','<=',date('Y-m-d 23:59:59')];
        $today_num=self::where($where)->count();
        $today_money=self::where($where)->sum('money');
        $total_out_money = $total_in_money = $total_ing_money = $total_performance = 0;
        if($tj_where){
            //支出
            $total_out_money = self::where($tj_where)->whereNotIn('withdraw_id',$withdraw_ids)->where(['type'=>-1])->sum('money');
            //收入
            $total_in_money = self::where($tj_where)->where(['type'=>1,'status'=>1,'withdraw_id'=>0])->sum('money');
            //待解冻
            $total_ing_money = self::where($tj_where)->where(['type'=>1,'status'=>0,'withdraw_id'=>0])->sum('money');
            //业绩
            $total_performance = self::where($tj_where)->where(['type'=>1,'withdraw_id'=>0])->sum('money');

        }

        return compact('all_num','all_money','today_money','today_num','total_out_money','total_in_money','total_ing_money','total_performance');
    }

    public static function in($money,$status,$member_id,$team_id,$order_id,$money_status=1){
        $PromotionrecordModel=new PromotionrecordModel();
        $p_s=[];
        $p_s['member_id']=$member_id;
        $p_s['team_id']=$team_id;
        $pmember_info=$PromotionrecordModel->where($p_s)->find();
        $old_money=0;
        if(empty($pmember_info)){
            $p_insert=[];
            $p_insert['member_id']=$member_id;
            $p_insert['team_id']=$team_id;
            $p_insert['money']=0;
            PromotionrecordModel::insert($p_insert);
        }else{
            $old_money=$pmember_info['money'];
        }
        $insert=[];
        $insert['team_id']=$team_id;
        $insert['member_id']=$member_id;
        $insert['order_id']=$order_id;
        $insert['type']=$status;
        $insert['old_money']=$old_money;
        $insert['money']=$money;
        $insert['status']=$money_status;
        $insert['new_money']=($old_money+$money);
        $insert['add_time']=date('Y-m-d H:i:s');
        if($status==0){
            $PromotionrecordModel->n_dec($p_s,'money',$money);
        }else{
            if($money_status==1){
                $PromotionrecordModel->n_inc($p_s,'money',$money);
            }
        }
        $where=[];
        $where['team_id']=$team_id;
        $where['member_id']=$member_id;
        $where['order_id']=$order_id;
        $where['type']=$status;
        $info=self::where($where)->find();
        if(empty($info)){
            $res=self::insertGetId($insert);
        }else{
            unset($insert['add_time']);
            $insert['edit_time']=date('Y-m-d H:i:s');
            $res=self::update($insert,['id'=>$info['id']]);
        }
        return $res;
    }
}
